package com.xiaobao.coalproperty.module.user.view;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xiaobao.coalproperty.MainActivity;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.user.model.entity.CenterIndexEntity;
import com.xiaobao.coalproperty.other.adapter.TopicAdapter;
import com.xiaobao.coalproperty.other.base.BaseFragment;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.DialogUtils;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.xiaobao.coalproperty.other.util.UserHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

public class UserFragment extends BaseFragment<MainActivity> {

    @BindView(R.id.recycler)
    XRecyclerView mXRecyclerView;

    private View headView;
    private TopicAdapter mTopicAdapter;
    private ImageView ivSet;
    private CircleImageView ivHead;
    private TextView tvServicePhone, tvName, tvAddress, tvMoney;
    private LinearLayout llUserData, llUserAuth, llUserPrope, llUserDeal, llUserCollect, llService, llAbout,llWithDraw;


    @Override
    protected int getContentView() {
        return R.layout.recycler;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        headView = LayoutInflater.from(getSuperActivity()).inflate(R.layout.fragment_user, null, false);
        ivSet = headView.findViewById(R.id.user_set);
        llUserData = headView.findViewById(R.id.user_data);
        llUserAuth = headView.findViewById(R.id.user_auth);
        llUserPrope = headView.findViewById(R.id.user_property);
        llUserDeal = headView.findViewById(R.id.user_deal);
        llUserCollect = headView.findViewById(R.id.user_collect);
        llService = headView.findViewById(R.id.user_service);
        llAbout = headView.findViewById(R.id.user_about);
        llWithDraw = headView.findViewById(R.id.user_withdraw);
        tvServicePhone = headView.findViewById(R.id.user_service_phone);
        tvName = headView.findViewById(R.id.user_tv_name);
        tvAddress = headView.findViewById(R.id.user_tv_address);
        tvMoney = headView.findViewById(R.id.user_tv_money);
        ivHead = headView.findViewById(R.id.user_iv_head);

        ivSet.setOnClickListener(mOnClickListener);
        llUserData.setOnClickListener(mOnClickListener);
        llUserAuth.setOnClickListener(mOnClickListener);
        llUserPrope.setOnClickListener(mOnClickListener);
        llUserDeal.setOnClickListener(mOnClickListener);
        llUserCollect.setOnClickListener(mOnClickListener);
        llService.setOnClickListener(mOnClickListener);
        llAbout.setOnClickListener(mOnClickListener);
        llWithDraw.setOnClickListener(mOnClickListener);

        mTopicAdapter = new TopicAdapter(getSuperActivity());

        mXRecyclerView.setLayoutManager(new LinearLayoutManager(getSuperActivity(), LinearLayoutManager.VERTICAL, false));
        mXRecyclerView.addHeaderView(headView);
        mXRecyclerView.setLoadingListener(mLoadingListener);
        mXRecyclerView.setAdapter(mTopicAdapter);

    }

    @Override
    public void onResume() {
        super.onResume();
        updateUser();
    }

    private XRecyclerView.LoadingListener mLoadingListener = new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            updateUser();
            mXRecyclerView.refreshComplete();
        }

        @Override
        public void onLoadMore() {
            mXRecyclerView.loadMoreComplete();
        }
    };

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.user_data:
                    startActivity(new Intent(getSuperActivity(), UserDataActivity.class));
                    break;
                case R.id.user_auth:
                    authClickEvent();
                    break;
                case R.id.user_property:
                    startActivity(new Intent(getSuperActivity(), UserPropertyActivity.class));
                    break;
                case R.id.user_set:
                    startActivity(new Intent(getSuperActivity(), UserSetActivity.class));
                    break;
                case R.id.user_deal:
                    startActivity(new Intent(getSuperActivity(), UserDealActivity.class));
                    break;
                case R.id.user_collect:
                    startActivity(new Intent(getSuperActivity(), UserCollectActivity.class));
                    break;
                case R.id.user_service:
                    serviceClickEvent();
                    break;
                case R.id.user_about:
                    startActivity(new Intent(getSuperActivity(), AboutActivtiy.class));
                    break;
                case  R.id.user_withdraw:
                    startActivity(new Intent(getSuperActivity(), WithdrawActivity.class));
                    break;

            }
        }
    };

    private void authClickEvent() {
        if (UserHelperUtils.getInstance().getCenter() != null) {
            if (UserHelperUtils.getInstance().getCenter().getData().getIs_certification().equals("3")) {
                showToast("您已经完成实名认证");
                return;
            }
            Intent intent = new Intent(getSuperActivity(), UserAuthActivity.class);
            intent.setAction("type");
            intent.putExtra("type", UserHelperUtils.getInstance().getCenter().getData().getIs_certification());
            startActivity(intent);
        }
    }

    private void updateUser() {
        OkHttpUtils.post().url(UrlConstant.URL_CENTER_INDEX)
                .params(HttpHelperUtils.getInstance().centerIndexParams())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    CenterIndexEntity entity = new Gson().fromJson(response, CenterIndexEntity.class);
                    if (entity.getCode() == 0) {
                        updateUserSuccess(entity);
                        return;
                    }
                    showToast(entity.getMsg());
                } catch (Exception e) {
                }
            }
        });
    }

    private void updateUserSuccess(CenterIndexEntity entity) {
        UserHelperUtils.getInstance().setCenter(entity);
        Glide.with(getSuperActivity()).load(entity.getData().getAvatar()).into(ivHead);
        tvName.setText(entity.getData().getNickname());
        tvAddress.setText(entity.getData().getArea_name() + "  " + entity.getData().getBirthday());
        tvMoney.setText(entity.getData().getCurrent_money());
    }

    private void serviceClickEvent() {
        DialogUtils.createConfirmDialog(getSuperActivity(), "提示", "是否要拨打服务电话？", "是", "否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                callPhone(tvServicePhone.getText().toString());
            }
        }, null, -1).show();
    }

    public void callPhone(String phoneNum) {
        try {
            Intent intent = new Intent(Intent.ACTION_CALL);
            Uri data = Uri.parse("tel:" + phoneNum);
            intent.setData(data);
            startActivity(intent);
        } catch (Exception e) {
        }
    }


}
